﻿#include "component.h"
#include "componentmanager.h"
#include "common_log.h"

Component::Component(QObject *parent)
	: QObject(parent)
{
	connect(this, &Component::PublishWarningEvent, m_ob, &ComponentManager::DealWithComponentWarningEvent);
	connect(this, &Component::PublishSystemEvent, m_ob, &ComponentManager::DealWithComponentSystemEvent);

}

Component::~Component()
{
}


void Component::SetErrmsg(const QString& str, bool send)
{
	m_errmsg = str;
	log_error("{}:{}", m_name, m_errmsg);
	if (send) {
		emit PublishSystemEvent((int)m_type, (int)ComponentEvent::system_event_stop);
	}
}

void Component::SetWarningmsg(const QString& str)
{
	QString m_errmsg = m_name + u8"告警事件:" + str;
	log_error("{}", m_errmsg);
	emit PublishWarningEvent((int)m_type, m_errmsg);
}

QString Component::GetComponentName()
{
	return m_name;
}
QString Component::GetComponentErrMsg()
{
	return m_errmsg;
}

ComponentType Component::GetComponentType()
{
	return m_type;
}

/***************************************************
* 初始化接口, 组件加载时调用
* 返回值判断， 0：表示成功  -1表示失败，程序可以继续， -99表示严重失败，程序停止
*************************************/
int Component::Init(const QByteArray& param)
{
	int ret = ComponentInit(param);
	if (ret == 0) {		// 组件初始化成功后，对组件信息进行校验
		if (m_name.isEmpty()) {
			SetWarningmsg("组件初始化异常：程序bug, 组件名称为空");
		}
		if (m_type <= ComponentType::component_null || m_type >= ComponentType::component_max) {
			SetWarningmsg("组件初始化异常：程序bug, 组件类型错误");
		}
	}

	return ret;
}

/***************************************************
* 组件复位接口, 清空组件错误
*
*************************************/
bool Component::Reset()
{
	m_errmsg.clear();
	return ComponentSelfReset();
}

/****************************************************************************
 * 功能: 组件状态检查
 * 传参:
 * 返回值:
 ****************************************************************************/
bool Component::CheckState()
{
	return ComponentCheckSelfState();
}
